/** Class Name: iFulfillQueryRedirectCtrl
* Author: Accenture IDC
* Date: 20-Dec-2011
* Requirement/Project Name: ifulfill
* Requirement/Project Description: Redirect to IR create page or out of region create page  based on FI Region type.
**/
public class iFulfillQueryRedirectCtrl 
{
    private Queries__c query {get;set;}
    private String recordType; 
    private id recId;
   
    /*
     * Constructor
     *
     **/
   
    public iFulfillQueryRedirectCtrl(ApexPages.StandardController controller) 
    {
           query = (Queries__c )controller.getRecord(); 
           recId= ApexPages.currentPage().getParameters().get('id');
    }
    
    /*
     * Redirect to query page based on FI region
     *
     **/ 
    public PageReference pageRedirect() 
    {
         PageReference newPage  = null;
         String recordType = null;
         List<Query_Request__C> queryReqList = [select id,fulfillment_item__c from query_request__c where id = :query.query_request_id__c ];
         RecordTypeIDs__c  recordTypeObj = RecordTypeIDs__c.getInstance(userinfo.getProfileId());
         ListViewIds__c listViewIDObj = ListViewIds__c.getInstance(userinfo.getProfileId());
         
         if(queryReqList.size() > 0)
         {    
            List <Profile> profileList  = [select name from profile  where id  = :userinfo.getProfileId()];     
            List<Fullfilment_item__c> fiList = [select Region__c from Fullfilment_item__c where id = :queryReqList.get(0).Fulfillment_Item__c];
            if(fiList.size() > 0)
            {
                Fullfilment_item__c fiObj = fiList.get(0);             
                String fiRegion = fiObj.Region__c;
                
                // Check FI region to redirect to IR create page else out of region create page
                          
                if(fiRegion == IfullfillTextConstant.FI_WORKFLOW_IE_REG || fiRegion == IfullfillTextConstant.FI_WORKFLOW_IW_REG ) 
                {
                    
                   newPage = new PageReference(IfulfillVFPagesName.IR_QUERY_CREATE_VIEW_PAGE);
                   recordType = recordTypeObj.IR_Record_Type__c;
                }
                else
                {
                  
                  // Check if user is assigned to SFA profile or IOG Teleuser Profile .
                  
                  

                 if(profileList.get(0).name.startsWith(IfullfillTextConstant.SFA_PROFILE_NAME_STARTS_WITH) || profileList.get(0).name.equalsIgnoreCase(IfullfillTextConstant.IOG_TELE_SALES_PROFILE_NAME))     
                 { 
                         newPage = new PageReference('/'+recId+'/e?nooverride=1&retURL='+recId); 
                 }
                 else
                 {
                       newPage = new PageReference(IfulfillVFPagesName.OOR_QUERY_CREATE_VIEW_PAGE);   
                        recordType = recordTypeObj.OOR_Record_Type__c;
                 
                                  }
                       
                }
              // Populate the URL parameter only if user is not part of SFA profile or IOG Teleuser Profile .. 
              if(!(profileList.get(0).name.startsWith(IfullfillTextConstant.SFA_PROFILE_NAME_STARTS_WITH) || profileList.get(0).name.equalsIgnoreCase(IfullfillTextConstant.IOG_TELE_SALES_PROFILE_NAME)))
              {
                  newPage.getParameters().put('CF'+listViewIDObj.Query_Request_Field_ID__c, ApexPages.currentPage().getParameters().get('CF'+listViewIDObj.Query_Request_Field_ID__c));
                  newPage.getParameters().put('CF'+listViewIDObj.Query_Request_Field_ID__c+'_lkid', ApexPages.currentPage().getParameters().get('CF'+listViewIDObj.Query_Request_Field_ID__c+'_lkid'));
                  newPage.getParameters().put('RecordType',recordType );
                  newPage.getParameters().put('scontrolCaching', '1');
                  newPage.getParameters().put('retURL', ApexPages.currentPage().getParameters().get('retURL'));
                  newPage.getParameters().put('nooverride', '1');   
                  newPage.getParameters().put('id', recId);                    
             }
              return newPage;
            }
        }
        // return the IR create page if user selected new button on tab home page.  
        return  new PageReference(IfulfillVFPagesName.IR_QUERY_CREATE_VIEW_PAGE);

    }
}